import processing.core.*; 
import processing.xml.*; 

import processing.net.*; 
import tacTile.net.*; 
import ddf.minim.*; 
import java.util.Calendar; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class project1 extends PApplet {







//Touch API
TouchAPI tacTile;

Calendar c = Calendar.getInstance();

//List of Touches
ArrayList touchList = new ArrayList();

//boolean to determine which Touch Server connected to.
boolean connectToTacTile = true;
boolean musicStarted=true;

//Names of machines you might use
String localMachine = "192.168.2.11";
String tacTileMachine = "tactile.evl.uic.edu";

//Port for data transferf
int dataPort = 7100;
int msgPort = 7340;

//Image for clear button
PShape bugShape;

player p1= new player(1);
player p2= new player(2);


AudioSample player,aPlayer;
Minim minim,minim1;

int vel=10; //velocity of the bugs

int step=0; // step=0: start screen,step=1: start game, step=3: finish screen

public void setup(){
  size(screen.width,screen.height);
  if ( connectToTacTile ){
      tacTile = new TouchAPI(this,7000);//, msgPort, localMachine);
        println("connected");

  } else {
      tacTile = new TouchAPI( this, 7000);
  }
  bugShape = loadShape("bot1.svg");
  minim = new Minim(this);
  minim1 = new Minim(this);
  aPlayer = minim.loadSample("h.mp3");
  player = minim1.loadSample("slap.mp3");
  aPlayer.trigger();
  
  //player.trigger();
  musicStarted=true;
  PFont font=loadFont("ComicSansMS-Bold-48.vlw");
}




public void draw(){
  background(0);
  switch (step){
      case 0: startScr();break;            
      case 1: startGame();break;
      case 2: levels(); break;
      case 3: finishScr();break;
      case 4: exit();
  }
}


public void startScr(){
  background(0);
  fill(124,252,0);
  rect(30,30,width-70,height-100);
  fill(0);
  ellipse((width/2),(height/2),100,100);
  ellipse((width/2),(height/2)+200,100,100);
  ellipse((width/2),(height/2)-200,100,100);
  PFont font=loadFont("ComicSansMS-Bold-48.vlw");
  textFont(font);
  fill(124,252,0);
  textSize(14);
  text("START",(width/2)-15,(height/2));
  text("HELP",(width/2)-15,(height/2)-200);
  text("LEVEL",(width/2)-15,(height/2)+200);
  touchList= tacTile.getManagedList();
  if ( ! tacTile.managedListIsEmpty()){
      for (int i=0;i< touchList.size();i++) {
          Touches curTouch = (Touches) touchList.get(i);
          //Touches curTouch = tacTile.getMostRecentTouchData();
          println(touchList.size());
          float xCoord = curTouch.getXPos() * width;    
          float yCoord = height - curTouch.getYPos() * height;
          fill(0);
          ellipse(xCoord,yCoord,30,30);
          if ((xCoord<=(width/2)+50) & (xCoord>=(width/2)-50) & (yCoord<=(height/2)+50) & (yCoord >=(height/2)-50)){
            step=1;
          }  
          if ((xCoord<=(width/2)+50) & (xCoord>=(width/2)-50) & (yCoord<=(height/2)+250) & (yCoord >=(height/2)+150)){
            step=2;
          }
            
      }
  }
}


public void startGame(){ 
      background(0);
      PFont font=loadFont("ComicSansMS-Bold-48.vlw");   
      fill(124,252,0);
      rect(30,30,width-70,height-80);
      fill(0);
      strokeWeight(10);
      line (width/2,0,width/2,height);   
      textFont(font);
      fill(124,252,0);
      textSize(20);
      text("SCORE:",80,20);text(String.valueOf(p1.score),180,20);
      text("SCORE:",width-180,20);text(String.valueOf(p2.score),width-70,20);
      launchBalls(1);
      launchBalls(2);
      touchList= tacTile.getManagedList();
      if ( ! tacTile.managedListIsEmpty()){
          for (int i=0;i< touchList.size();i++) {
              Touches curTouch = (Touches) touchList.get(i);
              //Touches curTouch = tacTile.getMostRecentTouchData(); 
              float xCoord = curTouch.getXPos() * width;    
              float yCoord = height - curTouch.getYPos() * height;
              fill(0);
              ellipse(xCoord,yCoord,40,40);
              for (int index=0;index<=11;index++){
                  if ((p1.xCoodBug[index]<=xCoord+50)&(p1.xCoodBug[index]>=xCoord-50) &&(p1.yCoodBug[index]<=yCoord+50) &(p1.yCoodBug[index]>=yCoord-50)){
                     killBug(1,index);
                     println(index);
                  }
                  if ((p2.xCoodBug[index]<=xCoord+50) & (p2.xCoodBug[index]>=xCoord-50) &&(p2.yCoodBug[index]<=yCoord+50) &(p2.yCoodBug[index]>=yCoord-50)){
                     killBug(2,index);
                      println(index);
                  }
              }              
          }
      }
       if ((p2.score >20 && p2.score %20 ==0) || (p1.score >20 && p1.score %20 ==0)) {aPlayer.stop();aPlayer.trigger();}
       if ((p1.score >=100 & p1.score<=108)||(p2.score>=100 && p2.score<=108)){
        fill(0);
        ellipse((width/2),(height/2),100,100);
        textFont(font);
        fill(124,252,0);
        textSize(24);
        text("Level 3",(width/2)-30,(height/2)); 
        vel=40;
        
      }else{
          if ((p1.score >=50 & p1.score<=58)||(p2.score>=50 && p2.score<=58)){
            fill(0);
            ellipse((width/2),(height/2),100,100);
            textFont(font);
            fill(124,252,0);
            textSize(24);
            text("Level 2",(width/2)-30,(height/2)); 
            vel=20;
          }else{
            if ((p1.score>=150 && p1.score<=158 ) || (p2.score>=150 && p2.score<=158 ) ){
              step=3;
            }
          }
      }
      

}
public void killBug(int playerId, int index){
      if (playerId==1){
          p1.xCoodBug[index]=0;
          p1.yCoodBug[index]=0;  
          p1.score+=2; 
          player.trigger(); 
      }else{
          println("bug killed");
          p2.xCoodBug[index]=0;
          p2.yCoodBug[index]=0;  
          p2.score+=2;
          player.trigger(); 
      }  
}

public void launchBalls(int i){
    if (i==1){
      for (int index=0;index<=11;index++){
          if (p1.xCoodBug[index]==0 && p1.yCoodBug[index]==0){
            float y=random(30,height-30);
            p1.xCoodBug[index]=0;
            p1.yCoodBug[index]=y;
            p1.increment=random(-10,10);
          }
      }
      for (int index=0;index<=11;index++){
          shape(bugShape, p1.xCoodBug[index], p1.yCoodBug[index], 50, 50);
           if (p1.xCoodBug[index]>=width/2 && p1.xCoodBug[index]<(width/2)+(vel/2)){p2.score++;}
          p1.xCoodBug[index]+=vel;
          p1.yCoodBug[index]+=(p1.direction * p1.increment)+2;
          if (p1.yCoodBug[index] < 30 || p1.yCoodBug[index]>=height-100 ){
                  p1.xCoodBug[index]=0;
                  p1.yCoodBug[index]=0;
          }
          
      }
   }else{
        for (int index=0;index<=11;index++){
          if (p2.xCoodBug[index]==0 && p2.yCoodBug[index]==0){
            float y=random(30,height-30);
            p2.xCoodBug[index]=width-(p1.xCoodBug[index]);
            p2.yCoodBug[index]=height-(p1.yCoodBug[index]);
          }
        }
       for (int index=0;index<=11;index++){
          if (p2.xCoodBug[index]<=width/2 && p2.xCoodBug[index]> width/2-(vel/2)){p1.score++;}
          shape ( bugShape,p2.xCoodBug[index] ,p2.yCoodBug[index] ,50, 50);
          p2.xCoodBug[index]=width-(p1.xCoodBug[index])-60;
          p2.yCoodBug[index]=height-(p1.yCoodBug[index])-60;
          if (p2.yCoodBug[index]<20 || p2.yCoodBug[index]>=height-80 ){
                  p2.xCoodBug[index]=0;
                  p2.yCoodBug[index]=0;
          }
          
       }      
   }  
   
}

public void levels(){
   
  musicStarted=true;
  background(0);
  fill(124,252,0);
  rect(30,30,width-70,height-100);
  fill(0);
  ellipse((width/2),(height/2),100,100);
  ellipse((width/2),(height/2)+200,100,100);
  ellipse((width/2),(height/2)-200,100,100);
  PFont font=loadFont("ComicSansMS-Bold-48.vlw");
  textFont(font);
  fill(124,252,0);
  textSize(14);
  text("LEVEL 1",(width/2)-30,(height/2));
  text("LEVEL 2",(width/2)-30,(height/2)-200);
  text("LEVEL 3",(width/2)-30,(height/2)+200);
  touchList= tacTile.getManagedList();
  if ( ! tacTile.managedListIsEmpty()){
      for (int i=0;i< touchList.size() ;i++) {
          Touches curTouch = (Touches) touchList.get(i);
          //Touches curTouch = tacTile.getMostRecentTouchData();
          println(touchList.size());
          float xCoord = curTouch.getXPos() * width;    
          float yCoord = height - curTouch.getYPos() * height;
          fill(0);
          ellipse(xCoord,yCoord,30,30);
          if ((xCoord<=(width/2)+50) & (xCoord>=(width/2)-50) & (yCoord<=(height/2)+50) & (yCoord >=(height/2)-50)){
            vel=20;
          }  
          if ((xCoord<=(width/2)+50) & (xCoord>=(width/2)-50) & (yCoord<=(height/2)-150) & (yCoord >=(height/2)-250)){
            vel=10;
          }
          if ((xCoord<=(width/2)+50) & (xCoord>=(width/2)-50) & (yCoord<=(height/2)+150) & (yCoord >=(height/2)+250)){
            vel=40;
          }  
      }
      step=1;
  }
  
}
public void finishScr(){
    background(0);
  fill(124,252,0);
  rect(30,30,width-70,height-100);
  fill(0);
  ellipse((width/2),(height/2),100,100);
  ellipse((width/2),(height/2)+200,100,100);
  ellipse((width/2),(height/2)-200,100,100);
  PFont font=loadFont("ComicSansMS-Bold-48.vlw");
  textFont(font);
  fill(124,252,0);
  textSize(14);
  text("PLAY AGAIN",(width/2)-40,(height/2));
  text("SAVE",(width/2)-15,(height/2)-200);
  text("EXIT",(width/2)-15,(height/2)+200);
  textSize(24); 
  textFont(font);
  fill(0);
  text(p1.score,(width/2)-300,height/2);
  textFont(font);
  text(p2.score,(width/2)+200,height/2);
  touchList= tacTile.getManagedList();
  if ( ! tacTile.managedListIsEmpty()){
      for (int i=0;i< touchList.size();i++) {
          Touches curTouch = (Touches) touchList.get(i);
          //Touches curTouch = tacTile.getMostRecentTouchData();
          println(touchList.size());
          float xCoord = curTouch.getXPos() * width;    
          float yCoord = height - curTouch.getYPos() * height;
          fill(0);
          ellipse(xCoord,yCoord,30,30);
          if ((xCoord<=(width/2)+50) && (xCoord>=(width/2)-50) && (yCoord<=(height/2)+50) && (yCoord >=(height/2)-50)){
            step=0;
          }  
          if (((xCoord<=(width/2)+50) && (xCoord>=(width/2)-50)) && ((yCoord<=(height/2)+250) && (yCoord >=(height/2)+150))){
            step=4;
          }
            
      }
  }
  
}

class player {
  public int score;
  public int numP;
  public float [] xCoodBug; 
  public float []  yCoodBug;
  public float increment;
  public float direction;
  player(int z){
    this.numP=z;
    xCoodBug= new float[12];
    yCoodBug= new float[12];
    for (int i=0;i<=4;i++){
      xCoodBug[i]=0;
      yCoodBug[i]=0;
    }
    score=0;
  }
}

  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#FFFFFF", "project1" });
  }
}
